//
//  PlayerManager.m
//  SleepingElf
//
//  Created by karong on 2023/10/17.
//

#import "PlayerManager.h"
#import "MusicManager.h"
#import "AppDelegate.h"
#import <AVFAudio/AVFAudio.h>

@interface PlayerManager ()
@property (nonatomic, strong) MusicManager *musicManager1;
@property (nonatomic, strong) MusicManager *musicManager2;
@property (nonatomic, strong) MusicManager *musicManager3;
///唤醒闹铃
@property (nonatomic, strong) MusicManager *musicManager4;
///保活
@property (nonatomic, strong) MusicManager *musicManager5;
@property (nonatomic, strong) NSMutableArray * managerArray;
@end

@implementation PlayerManager
SingletonM

- (void)playWithMusic:(MusicModel *)music {
    for (int i = 0; i < 3; i++) {
        MusicManager * manager = self.managerArray[i];
        if (i == 0) {
            [manager playWithMusic:music];
        }
        else {
            [manager stop];
            manager.music = [MusicModel new];
        }
    }
    if (music) {
        appConfig.lastMusicArray = @[music];
        self.musicList = [NSArray new];
        [[NSNotificationCenter defaultCenter]postNotificationName:SERefreshMusicNotification object:nil];
    }
}

- (void)playWithMusics:(NSArray *)musicArray {
    for (int i = 0; i < 3; i++) {
        MusicManager * manager = self.managerArray[i];
        if (i < musicArray.count) {
            MusicModel * music = musicArray[i];
            [manager playWithMusic:music];
        }
        else {
            [manager stop];
            manager.music = [MusicModel new];
        }
    }
    appConfig.lastMusicArray = musicArray;
    self.musicList = [NSArray new];
    [[NSNotificationCenter defaultCenter]postNotificationName:SERefreshMusicNotification object:nil];
}

- (void)playListWithMusic:(MusicModel *)music {
    for (int i = 0; i < 3; i++) {
        MusicManager * manager = self.managerArray[i];
        if (i == 0) {
            [manager playWithMusic:music];
        }
        else {
            [manager stop];
            manager.music = [MusicModel new];
        }
    }
    if (music) {
        appConfig.lastMusicArray = @[music];
        [[NSNotificationCenter defaultCenter]postNotificationName:SERefreshMusicNotification object:nil];
    }
}

- (void)pause {
    for (MusicManager * manager in self.managerArray) {
        [manager pause];
    }
}

- (void)resume {
    for (MusicManager * manager in self.managerArray) {
        [manager resume];
    }
}

- (BOOL)canAction:(BOOL)isPrevious {
    BOOL rec = YES;
    if (self.musicList.count < 2) {
        rec = NO;
    }
    else {
        NSInteger playStatus = appConfig.playStatus % 4;
        switch (playStatus) {
            case 0:
                if (isPrevious) {
                    rec = !(self.musicList.firstObject == [self currentPlayingMusic]);
                }
                else {
                    rec = !(self.musicList.lastObject == [self currentPlayingMusic]);
                }
                break;
            case 2:
                rec = NO;
                break;
            default:
                break;
        }
    }
    
    return rec;
}

- (MusicModel *)cutMusic:(BOOL)isPrevious {
    NSInteger playStatus = appConfig.playStatus % 4;
    MusicModel * music;
    switch (playStatus) {
        case 0:
        case 1:
            if (isPrevious) {
                NSInteger index = [self.musicList indexOfObject:[self currentPlayingMusic]];
                NSInteger previousIndex = (index + self.musicList.count - 1) % self.musicList.count;
                music = self.musicList[previousIndex];
            }
            else {
                NSInteger index = [self.musicList indexOfObject:[self currentPlayingMusic]];
                NSInteger nextIndex = (index + 1) % self.musicList.count;
                music = self.musicList[nextIndex];
            }
            break;
        case 3:
            music = self.musicList[arc4random()%self.musicList.count];
            break;
        default:
            break;
    }
    return music;
}

- (BOOL)previous {
    if ([self canAction:YES]) {
        [self playListWithMusic:[self cutMusic:YES]];
        [((AppDelegate*)([UIApplication sharedApplication].delegate)) updateNowPlayingInfo];
    }
    return [self canAction:YES];
}

- (BOOL)next {
    if ([self canAction:NO]) {
        [self playListWithMusic:[self cutMusic:NO]];
        [((AppDelegate*)([UIApplication sharedApplication].delegate)) updateNowPlayingInfo];
    }
    return [self canAction:NO];
}

- (BOOL)isPlaying {
    BOOL rec = NO;
    for (MusicManager * manager in self.managerArray) {
        if (manager.isPlaying) {
            rec = YES;
            break;
        }
    }
    return rec;
}

- (BOOL)musicIsPlaying:(MusicModel *)music {
    BOOL rec = NO;
    for (MusicManager * manager in self.managerArray) {
        if (manager.music.id == music.id) {
            rec = manager.isPlaying;
            break;
        }
    }
    return rec;
}

- (MusicModel *)currentPlayingMusic {
    MusicModel * music;
    for (MusicManager * manager in self.managerArray) {
        if (manager.music.id > 0) {
            music = manager.music;
            break;
        }
    }
    return music;
}

- (CGFloat)playerVolumeWithMusic:(MusicModel *)music {
    for (MusicManager * manager in self.managerArray) {
        if (manager.music.id == music.id) {
            return manager.playerVolume;
        }
    }
    return 0;
}

- (void)defaultMusic:(MusicModel *)music {
    if (!self.musicManager1.music) {
        self.musicManager1.music = music;
        appConfig.lastMusicArray = @[music];
    }
}

- (void)changePlayerVolume:(float)volume music:(MusicModel *)music {
    for (MusicManager * manager in self.managerArray) {
        if (manager.music.id == music.id) {
            [manager changePlayerVolume:volume];
            [[NSNotificationCenter defaultCenter]postNotificationName:SERefreshMusicNotification object:nil];
        }
    }
}

- (void)keepAlive {
    [self.musicManager5 keepAlivePlay];
}

- (void)wakeUpWithMusicUrl:(NSString *)musicUrl {
    MusicModel * music = [MusicModel new];
    music.url = musicUrl;
    [self.musicManager4 playWithMusic:music];
}

- (void)stopWakeUp {
    [self.musicManager4 stop];
    [self.musicManager5 stop];
}

- (NSMutableArray *)managerArray {
    if (!_managerArray) {
        _managerArray = [[NSMutableArray alloc]initWithObjects:self.musicManager1, self.musicManager2, self.musicManager3, nil];
        for (int i = 0; i < 3; i++) {
            if (i < appConfig.lastMusicArray.count) {
                MusicModel * music = appConfig.lastMusicArray[i];
                MusicManager * manager = _managerArray[i];
                manager.music = music;
            }
        }
    }
    return _managerArray;
}

- (MusicManager *)musicManager1 {
    if (!_musicManager1) {
        _musicManager1 = [MusicManager new];
    }
    return _musicManager1;
}

- (MusicManager *)musicManager2 {
    if (!_musicManager2) {
        _musicManager2 = [MusicManager new];
    }
    return _musicManager2;
}

- (MusicManager *)musicManager3 {
    if (!_musicManager3) {
        _musicManager3 = [MusicManager new];
    }
    return _musicManager3;
}

- (MusicManager *)musicManager4 {
    if (!_musicManager4) {
        _musicManager4 = [MusicManager new];
    }
    return _musicManager4;
}

- (MusicManager *)musicManager5 {
    if (!_musicManager5) {
        _musicManager5 = [MusicManager new];
        _musicManager5.type = 2;
    }
    return _musicManager5;;
}
@end
